<?php
$this->title = '评论';

$css = <<<CSS
.m-add-comm dl.comment-choice {
    margin-top: .875rem;
    background-color: #fff;
    padding: .625rem 1rem;
    font-size: .875rem;
}
CSS;
$this->registerCss($css);

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>评价</title>
    <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=0" />
    <meta name="format-detection" content="telephone=no,email=no" />
    <meta name="mobileOptimized" content="width" />
    <meta name="handheldFriendly" content="true" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <link rel="stylesheet" href="/static/css/main.css" />
    <script src="/static/script/zepto.min.js"></script>
</head>
<body class="m-add-comm">

<div class="desc f-cb">
    <?php if(!empty($data['product'])){?>
    <img src=<?= $data['product']['thumb']?> />
    <div>
        <a href="javascript:;"><?= $data['product']['name']?></a>
        <?php if(!empty($data['product']['attributes'])){
            foreach($data['product']['attributes'] as $key => $val){
                if($key != 'product_id'){ ?>
                    <span><?= $key?>：<?= $val?></span>
                <?php }
            }
        }?>
        <b><?= $data['product']['price']?></b>
        <em>交易成功</em>
    </div>
    <?php } ?>
</div>

<?php if (!empty($d_comment)) {?>
    <dl class="tit f-cb"><dt>请对一下项目进行评价：</dt></dl>
    <?php foreach($d_comment as $k => $v) {?>
        <dl class="choice f-cb"><dt class="d_title_q" result_type="0" d_id="<?= $v['d_id']?>">商品的<?= $v['name']?></dt><dd><span><em style="width:0%;"></em></span></dd>
            <dt class="d_comment"><a result_type="1">满意</a> <label></label></dt>
            <dt class="d_comment"><a result_type="2">一般</a> <label></label></dt>
            <dt class="d_comment"><a result_type="3">不满意</a> <label></label></dt>
        </dl>
    <?php }?>
<?php }?>

<div class="form">
    <textarea id="J_inputCnt" rows="6" placeholder="请输入反馈意见"></textarea>
    <span>0 字</span>
</div>

<div class="upld">

    <button type="submit" onclick="submit();">提交</button>
</div>

<script>
    $(function(){

        $('#J_inputCnt').on('keyup', function() {
            var _this = $(this);
            _this.next().text(_this.val().length + ' 字');
        });

        function upfile(ob){

            var dir = ob.value;
            if(dir.length <= 0) return !1;

            var ext = /\.[^\.]+$/.exec(dir)[0];

            if(ext !== '.jpg' || ext !== '.png' || ext !== '.gif'){
                console.log('格式错误');
                return !1;
            }

            // ......
        }

        $('.d_comment a').on('click', function() {
            $(this).parent().parent().each(function() {
                $(this).find('label').text('');
            });
            $(this).next('label').text('√');

            $result_type = $(this).attr('result_type');
            $(this).parent().parent().children('.d_title_q').attr('result_type', $result_type);
            $(this).parent().parent().find('em').css('width', (99 - 33 * ($result_type - 1)) + '%');
        });
    });
    function submit()
    {
        var text = $('#J_inputCnt').val();
//        if ($.trim(text) == '') {
//            msgPopup('请填入评论信息！');
//            return !1;
//        }
        var pid = <?= $data['product_id']?>;
        var oid = <?= $order_id?>;

        var d_comment = {};
        var i = 0;
        $(".d_title_q").each(function() {
            var one_comment = {};
            one_comment.d_id = $(this).attr('d_id');
            one_comment.result_type = $(this).attr('result_type');
            d_comment[i] = one_comment;
            i++;
        });
        //如果有评论项目
        if (i != 0) {
            for(var j in d_comment){
                if (d_comment[j]['result_type'] == 0) {
                    msgPopup('请对评论项进行评论！');
                    return !1;
                }
            }
        }
        $.get("<?=\yii\helpers\Url::to(['add'])?>", {pid:pid, content:text, order_id:oid, d_comment:d_comment}, function(data) {
            if(data == 1) {
                msgPopup("感谢您对商品的评价~");
                setTimeout(function() {
                    window.location.href = '/web/order';
                }, 1500);

            }else {
                msgPopup(data);
            }
        })
    }
</script>
</body>
</html>